#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
from multiprocessing import Process
import requests
import json

# 压测平台推流工具服务ip地址
LOAD_PLATFORM = "11.160.94.123:8080"


def get_account_list(index, appid="1400143280"):
    url = "http://{}/api/v1/get_account_id".format(LOAD_PLATFORM)
    params = {
        "appid": appid
    }
    headers = {
        "Content-Type": "application/json"
    }
    res = requests.post(url, data=json.dumps(params), headers=headers)
    rsp_data = res.content
    if res.status_code == 201:
        rsp_info = json.loads(rsp_data)
        account_id = rsp_info.get("data").get("account_id")
        print "account_id:{}, index:{}".format(account_id, index)
        return account_id
    else:
        print "get_account_list failed  url:{}, params:{}, rsp_data:{}".format(url, params, rsp_data)
        return None


def main():
    start = time.time()
    # print("main current pid %d" % os.getpid())
    # print("main parent pid %d" % os.getppid())
    # args = ('test', 'libo', 'jack')
    process_list = []
    # python中的多进程
    for i in range(1000):
        p = Process(target=get_account_list, args=(i,))
        p.start()
        process_list.append(p)
    for j in process_list:
        j.join()
    end = time.time()
    print("total cost time %.2f" % (end - start))


if __name__ == '__main__':
    main()
